/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019.
 * Description: add chip handle for hisi soc
 * Author: zhangwen
 * Create: 2019-09-14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <asm/io.h>
#include <linux/of_address.h>
#include <linux/of_fdt.h>

#include <mach/kexport.h>

hi_chip_id_e hi_chip_id = HI_CHIP_ID_NONE_E;
EXPORT_SYMBOL(hi_chip_id);

static hi_chip_id_e do_get_chip_id(unsigned int chip_type, unsigned int chip_sub_type)
{
	switch (chip_type) {
	case CHIP_ID_SD5610H:
		return HI_CHIP_ID_5610H_E;
	case CHIP_ID_SD5610T:
		return HI_CHIP_ID_5610T_E;
	case CHIP_ID_SD5116H:
		return HI_CHIP_ID_5116H_E;
	case CHIP_ID_SD5116S:
		return HI_CHIP_ID_5116S_E;
	case CHIP_ID_SD5116T:
		return HI_CHIP_ID_5116T_E;
	case CHIP_ID_SD5116L:
		return HI_CHIP_ID_5116L_E;
	case CHIP_ID_SD5115H:
		return HI_CHIP_ID_5115H_E;
	case CHIP_ID_SD5115S:
		return HI_CHIP_ID_5115S_E;
	case CHIP_ID_SD5115T:
		return HI_CHIP_ID_5115T_E;
	case CHIP_ID_SD5117H:
		return HI_CHIP_ID_5117H_E;
	case CHIP_ID_SD5117P:
		return HI_CHIP_ID_5117P_E;
	default:
		break;
	}

	switch (chip_sub_type) {
	case CHIP_ID_SD5118:
		return HI_CHIP_ID_5118_E;
	case CHIP_ID_SD5118V2:
		return HI_CHIP_ID_5118V2_E;
	default:
		break;
	}

	return HI_CHIP_ID_MPW_E;
}

static hi_chip_id_e get_chip_id(void)
{
	unsigned int chip_type, chip_sub_type;
	unsigned int reg;
	void __iomem *sysctl = NULL;
	struct device_node *np = NULL;

	np = of_find_compatible_node(NULL, NULL, "arm,sp810");
	sysctl = of_iomap(np, 0);
	if (!sysctl) {
		pr_err("ioremap sysctl failed\n");
		return HI_CHIP_ID_MPW_E;
	}

	reg = readl(sysctl + 0x0800);
	iounmap(sysctl);

	chip_type = reg & CHIP_ID_SERIAL_VERSION_MASK;
	chip_sub_type = reg & CHIP_ID_SUB_VERSION_MASK;

	return do_get_chip_id(chip_type, chip_sub_type);
}

hi_chip_id_e hi_kernel_get_chip_id(void)
{
	if (HI_CHIP_ID_NONE_E == hi_chip_id)
		hi_chip_id = get_chip_id();

	return hi_chip_id;
}
EXPORT_SYMBOL(hi_kernel_get_chip_id);

